package com.enterprise.controller.api.caseapi;

import com.alibaba.fastjson.JSON;
import com.enterprise.controller.api.caseapi.vo.CaseResp;
import com.enterprise.controller.api.caseapi.vo.CaseTypeResp;
import com.enterprise.controller.api.common.BaseResponse;
import com.enterprise.entity.Case;
import com.enterprise.entity.CaseType;
import com.enterprise.entity.page.PageModel;
import com.enterprise.service.CaseService;
import com.enterprise.service.CaseTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RequestMapping("/api/case")
@Controller("apiCaseController")
public class CaseController {

    @Autowired
    private CaseService caseService;

    @Autowired
    private CaseTypeService caseTypeService;

    @ResponseBody
    @RequestMapping("/queryCaseTypeList")
    public BaseResponse<List<CaseTypeResp>> queryCaseTypeList(){
        BaseResponse response = new BaseResponse();
        List<CaseType> caseList = caseTypeService.selectList(new CaseType());
        List<CaseTypeResp> caseTypeResps = new ArrayList<>();
        for (CaseType caseType:caseList) {
            CaseTypeResp caseResp = new CaseTypeResp();
            BeanUtils.copyProperties(caseType,caseResp);
            caseTypeResps.add(caseResp);
        }
        response.setData(caseTypeResps);
        return response;
    }


    @ResponseBody
    @RequestMapping("/queryCasePage")
    public BaseResponse<PageModel> queryMessagePage(@RequestBody Case cas){
        log.info("queryCasePage cas: {}" , JSON.toJSONString(cas));
        BaseResponse response = new BaseResponse();
        if (cas.getOffset() <= 0 || cas.getPageSize() <= 0){
            return BaseResponse.fail("分页参数必须大于0");
        }
        PageModel pageModel = caseService.selectPage(cas);
        List<Case> caseList = pageModel.getList();
        List<CaseResp> caseResps = new ArrayList<>();
        for (Case cas1:caseList) {
            CaseResp caseResp = new CaseResp();
            BeanUtils.copyProperties(cas1,caseResp);
            caseResp.setImage(BaseResponse.httpstr(caseResp.getImage()));
            caseResp.setLinkUrl(BaseResponse.httpstr(caseResp.getLinkUrl()));
            caseResps.add(caseResp);
        }
        pageModel.setList(caseResps);
        log.info("queryCasePage pageModel: {}" , JSON.toJSONString(pageModel));
        response.setData(pageModel);
        return response;
    }


    @ResponseBody
    @RequestMapping("/queryCaseList")
    public BaseResponse<List<CaseResp>> queryCarList(@RequestBody Case casreq){
        BaseResponse response = new BaseResponse();
        if (casreq == null){
            casreq = new Case();
        }
        List<Case> caseList = caseService.selectList(casreq);
        List<CaseResp> caseResps = new ArrayList<>();
        for (Case cas:caseList) {
            CaseResp caseResp = new CaseResp();
            BeanUtils.copyProperties(cas,caseResp);
            caseResp.setImage(BaseResponse.httpstr(caseResp.getImage()));
            caseResp.setLinkUrl(BaseResponse.httpstr(caseResp.getLinkUrl()));
            caseResps.add(caseResp);
        }
        log.info("queryCaseList caseResps: {}" , JSON.toJSONString(caseResps));
        response.setData(caseResps);
        return response;
    }


    @ResponseBody
    @RequestMapping("/queryCaseView/{id}")
    public BaseResponse<CaseResp> queryCaseView(@PathVariable("id") String id){
        BaseResponse response = new BaseResponse();
        Case cas = caseService.selectById(Integer.valueOf(id));
        CaseResp caseResp = new CaseResp();
        BeanUtils.copyProperties(cas,caseResp);
        caseResp.setImage(BaseResponse.httpstr(caseResp.getImage()));
        caseResp.setLinkUrl(BaseResponse.httpstr(caseResp.getLinkUrl()));
        log.info("queryCaseView caseResp: {}" , JSON.toJSONString(caseResp));
        response.setData(caseResp);
        return response;
    }

}
